﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html;
using System.Collections;
using iTextSharp.text.html.simpleparser;

namespace PDFStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("pdf文档正在生成。。。");
            //Rectangle pageSize = new Rectangle(PageSize.A5);
            //pageSize.BackgroundColor = BaseColor.WHITE;
            //Document doc = new Document(pageSize);
            //PdfWriter.GetInstance(doc,new FileStream("first.pdf",FileMode.Create));
            //doc.Open();
            //doc.AddTitle("Title Information");
            ////doc.Add(Watermark );
            //doc.Add(new Paragraph("Welcome"));
            //doc.Add(new Paragraph("To"));
            //doc.Add(new Paragraph("QST."));
            //Chunk chunk = new Chunk("hello world", FontFactory.GetFont(FontFactory.COURIER,10,Font.UNDERLINE));
            //doc.Add(chunk);
            //doc.Close();

            //HtmlParser.Parse();
            //Console.ReadLine();

            //Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            //try
            //{
            //    PdfWriter writer = PdfWriter.GetInstance(document,
            //    new FileStream("test.pdf ",FileMode.OpenOrCreate));
            //    //document.Open();
            //    HtmlParser.Parse(document, "http://localhost:2011/Default.aspx");
            //    /*
            //    读取本程序目录下的a.htm文件，可以换成网站的文件位置，如http://localhost:8080/a.htm

            //    */
            //}
            //catch (DocumentException de)
            //{
            //   // System.err.println(de.getMessage());
            //}
            ////document.Close();


            FileStream m = new FileStream("aaa.pdf",FileMode.OpenOrCreate);
            Document document = new Document();
            HTMLWorker worker = new HTMLWorker(document);

            System.Net.WebClient MyWebClient = new System.Net.WebClient();
            MyWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Byte[] pageData = MyWebClient.DownloadData("http://localhost:2011/s.htm");
            string pageHtml = System.Text.Encoding.Default.GetString(pageData);
            TextReader fileReader = new StringReader(pageHtml);
            //Response.Write(pageHtml);
            //Response.End();

            try
            {
                //Response.ContentType = "application/pdf ";
                //Response.AddHeader("Content-disposition ", "attachment;       filename= " + BaseRequest.GetString("espno ") + ".pdf ");
                //   FileReader   fileReader   =   new   FileReader( "chinese.html ");
                PdfWriter.GetInstance(document, m);
                //BaseFont   bf   =   BaseFont.CreateFont( "C:\\WINDOWS\\Fonts\\ARIAL.TTF ",   BaseFont.IDENTITY_H,   BaseFont.NOT_EMBEDDED);
                //Font   font1   =   new   Font(bf,   12,   Font.NORMAL);
                document.Open();
                StyleSheet styles = new StyleSheet();

                //styles.LoadStyle();

                Hashtable hashpars = new Hashtable();
                worker.Style = styles;
                worker.InterfaceProps = hashpars;
                worker.Parse(fileReader);
            }
            catch (DocumentException ex)
            {
                Console.Error.WriteLine(ex.StackTrace);
                Console.Error.WriteLine(ex.Message);
            }
            //       step       5:       Close       document          
            document.Close();

        }
    }
}
